#include "socketinfo.h"

static struct connection * _g_free_socke_info = 0;
static int socket_id = 0;

struct connection * alloc_connection(struct event_base * base, int fd)
{
	struct connection * conn = 0;

	if (_g_free_socke_info == 0) {
		conn = malloc(sizeof(struct connection));
	} else {
		conn = _g_free_socke_info;
		_g_free_socke_info = conn->next;
	}

	if (conn) {
		conn->fd = fd;
		conn->id = ++socket_id;
		conn->bev = bufferevent_socket_new(base, fd,
				BEV_OPT_CLOSE_ON_FREE);
		conn->ctx = 0;
	}
	return conn;
}

void release_connection(struct connection * conn)
{
	bufferevent_free(conn->bev);
	conn->next = _g_free_socke_info;
	_g_free_socke_info = conn;
}
